import sqlalchemy

from autotest.models.base import Base
from sqlalchemy.orm import aliased, mapped_column
from sqlalchemy import Integer, String, Text, DateTime, BigInteger, func, \
    distinct, text, and_, JSON, DECIMAL, select, update, Boolean, case
from autotest.schemas.api.data_factory import ApiQuery


class DataFactoryApi(Base):
    """造数场景接口信息"""
    __tablename__ = 'df_api_info'

    sence_name = mapped_column(String(255), nullable=False, comment="造数场景名称", index=True)
    api_org_id = mapped_column(BigInteger, nullable=False, comment='所属域id', index=True)
    api_org_module_id = mapped_column(BigInteger, nullable=False, comment='所属子域id')
    api_type = mapped_column(Integer, comment='造数接口状态 1 post ， 2 get', default=10)
    api_status = mapped_column(Integer, comment='造数场景状态 10, 生效 ， 20 失效', default=10)
    req_url = mapped_column(String(255), nullable=False, comment='请求地址')
    req_method = mapped_column(String(20), comment='请求方式')
    req_headers = mapped_column(JSON, comment='请求头')
    req_data_moudle = mapped_column(JSON, comment='请求参数模板')
    remarks = mapped_column(String(255), comment='描述')

    @classmethod
    async def get_list(cls, params: ApiQuery):
        q = [cls.enabled_flag == 1]
        if params.id:
            q.append(cls.id == params.id)
        if params.sence_name:
            q.append(cls.sence_name.like('%{}%'.format(params.sence_name)))

        sort_type = 'asc' if params.id == 0 else 'desc'

        stmt = select(cls.id,
                      cls.sence_name).where(*q)
        print(f"stmt: {stmt}")
        return await cls.pagination(stmt)


class DataFactoryOrg(Base):
    """造数-组织信息"""
    __tablename__ = 'df_organization_info'

    organization_name = mapped_column(String(20), nullable=False, comment="域名称", index=True)
    organization_type = mapped_column(Integer, comment='造数接口状态 1 主域 ， 2 子域', default=10)
    organization_module_id = mapped_column(BigInteger, nullable=False, comment='主域id')
    organization_status = mapped_column(Integer, comment='造数场景状态 10, 生效 ， 20 失效', default=10)
    remarks = mapped_column(String(255), comment='描述')

# class DataFactoryOrg(Base):
#     """造数-组织信息"""
#     __tablename__ = 'df_organization_info'
#
#     organization_name = mapped_column(String(20), nullable=False, comment="域名称", index=True)
#     organization_type = mapped_column(Integer, comment='造数接口状态 1 主域 ， 2 子域', default=10)
#     organization_module_id = mapped_column(BigInteger, nullable=False, comment='主域id')
#     organization_status = mapped_column(Integer, comment='造数场景状态 10, 生效 ， 20 失效', default=10)
#     remarks = mapped_column(String(255), comment='描述')

# if __name__ == '__main__':
#     # from celery_worker.scheduler import DatabaseScheduler
#     # db = DatabaseScheduler()
#     engine = sqlalchemy.create_engine('mysql+pymysql://root:admin123@localhost/zerorunner', echo=True)
#
#     Base.metadata.create_all(engine)
